/*
----------------------------------------------------------------------------------------------------
This source file is part of the Gamut Framework
Copyright (c) 2007 by Ryan Holcombe

This program is free software; you can redistribute it and/or modify it under the terms of the GNU
Lesser General Public License as published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
----------------------------------------------------------------------------------------------------
*/

#include "GamutException.h"
#include "GamutLogger.h"
#include <sstream>
#include <iostream>

namespace Gamut
{
	// -------------------------------------------------------------------------------------------//
	Exception::Exception(const std::string& function, const std::string& msg) : 
		mFunction(function), mMsg(msg)
	{
		std::stringstream sstream;
		sstream << "An Exception has Occurred! Function: " << mFunction << ", Desc: " << mMsg;
		mFullError = sstream.str();

// if defined, try and log this Exception
#ifdef EXCEPTION_TO_LOG
		Gamut::Logger *logger = Gamut::Logger::getSingletonPtr();
		if (logger)
			logger->logEvent(mFunction, 0, EXCEPTION, mMsg);
#endif

// if defined, output this error to stderr
#ifdef EXCEPTION_TO_STDERR
		std::cerr << mFullError << std::endl;
#endif
	}

	// -------------------------------------------------------------------------------------------//
	Exception::Exception(const std::string& function, const char* msg, ...) :
		mFunction(function)
	{
		std::string s;
		char buf[1024];

		// build the msg
		va_list args;
		va_start(args, msg);
		vsnprintf(buf, sizeof(buf), msg, args);
		va_end(args);
		s = std::string(buf);

		Exception(function, s);
	}
}
